package com.xy.mainp.main.activity;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.KeyEvent;

import com.xy.base.reciver.ExitReciver;
import com.xy.base.reciver.NetStateReciver;
import com.xy.mainp.ActivityWelComeBinding;
import com.xy.mainp.R;
import com.xy.mainp.base.BasePActivity;
import com.xy.debug.SystemConfig;
import com.xy.net.NetConnectUtils;

/**
 * Created by John on 2016/10/19.
 */

public class WelcomeActivity extends BasePActivity {

    private ActivityWelComeBinding binding;
    private Handler handler = new Handler(Looper.getMainLooper());
    private NetConn netConn;

    @Override
    protected void initView(Bundle savedInstanceState) {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_welcome);
        String appVersionName = SystemConfig.getAppVersionName(this);
        binding.tvVersionWelcome.setText(String.format(getString(R.string.version_code), appVersionName));
        netConn = new NetConn();
        registerReceiver(netConn,NetConn.getIntentFilter());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(netConn);
    }

    private Runnable runable = new Runnable() {
        @Override
        public void run() {
            MainActivity.enter(new Intent(), WelcomeActivity.this);
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            handler.removeCallbacks(runable);
            handler = null;
            ExitReciver.exit(this);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    class NetConn extends NetStateReciver {
        @Override
        public void onReceive(Context context, Intent intent) {
            super.onReceive(context, intent);
            if (NetConnectUtils.getInstance(WelcomeActivity.this).isNetWorkConnet()) {
                handler.postDelayed(runable, 800);
            }
        }
    }
}
